package com.niit.user.web;


import com.niit.user.pojo.User;
import com.niit.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author yaojianfeng
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${server.port}")
    private String serverPort;

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) {
        User user = userService.queryById(id);
        user.setServerPort(serverPort);
        return user;
    }


    @PostMapping("/update")
    public User queryById(@RequestBody User user) {
        return userService.updateById(user);
    }

    @GetMapping("/findUser/{id}")
    public User findUser(@PathVariable("id") String id) {
        Long userId = Long.parseLong(id);
        User user = userService.queryById(userId);
        user.setServerPort(serverPort);
        return user;
    }

    @RequestMapping("/findAll/{ids}")
    public List<User> findAll(@PathVariable String ids) {
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        List<User> userList =  userService.findAll(strings);
        userList.forEach(user -> user.setServerPort(serverPort));
        return userList;
    }
}
